package com.mlethe.library.multi.demo;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.mlethe.library.multi.demo.databinding.ActivityShareBinding;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 分享
 */
public class ShareActivity extends AppCompatActivity {

    private ActivityShareBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityShareBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        binding.wechatBtn.setOnClickListener(v -> {
            Intent intent = new Intent(this, WechatShareActivity.class);
            startActivity(intent);
        });
        binding.qqBtn.setOnClickListener(v -> {
            Intent intent = new Intent(this, QQShareActivity.class);
            startActivity(intent);
        });
        binding.alipayBtn.setOnClickListener(v -> {
            Intent intent = new Intent(this, AlipayShareActivity.class);
            startActivity(intent);
        });
        binding.weiboBtn.setOnClickListener(v -> {
            Intent intent = new Intent(this, WeiboShareActivity.class);
            startActivity(intent);
        });
        copy();
    }

    private void copy() {
        File externalFilesDir = getExternalFilesDir(null);
        File externalCacheDir = getExternalCacheDir();
        copyFile("eeee.mp4", new File(externalFilesDir, "file_video"));
        copyFile("aaa.png", externalFilesDir);
        copyFile("bbb.jpg", externalCacheDir);
        copyFile("ccc.JPG", new File(externalFilesDir, "file_images"));
        copyFile("ddd.jpg", new File(externalFilesDir, "cache_images"));
        copyFile("eee.jpg", new File(externalCacheDir, "file_images"));
        copyFile("fff.jpg", new File(externalFilesDir, "file_images"));
        copyFile("ggg.JPG", new File(externalFilesDir, "file_images"));
        copyFile("hhhh.jpg", new File(externalFilesDir, "file_images"));
        copyFile("kkk.JPG", new File(externalFilesDir, "file_images"));
    }

    private void copyFile(final String fileName,File fileDir) {
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
        final File file = new File(fileDir, fileName);
        if (!file.exists()) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try (InputStream inputStream = getAssets().open(fileName);
                         OutputStream outputStream = new FileOutputStream(file)){
                        byte[] buffer = new byte[1444];
                        int readSize;
                        while ((readSize = inputStream.read(buffer)) != 0) {
                            outputStream.write(buffer, 0, readSize);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            thread.start();
        }
    }
}
